package us.versus.them.intersect;

class Point {
	public var x : Float;
	public var y : Float;
	public var z : Float;

	public function new( ?x:Float, ?y:Float, ?z:Float ) {
		this.assign( x, y, z );
	}

	public inline function normalize() : Point {
		var d2 = this.dot( this );
		if ( 0 != d2 ) {
			this.scale( 1 / Math.sqrt( d2 ) );
		}
		return this;
	}

	public inline function assign( ?x:Float, ?y:Float, ?z:Float ) : Point {
		this.x = x;
		this.y = y;
		this.z = z;
		return this;
	}

	public inline function add( a:Point, b:Point ) : Point {
		this.x = a.x + b.x;
		this.y = a.y + b.y;
		this.z = a.z + b.z;
		return this;
	}

	public inline function scale( v:Float ) : Point {
		this.x *= v;
		this.y *= v;
		this.z *= v;
		return this;
	}

	public inline function toString() {
		return 'x="' + x + '" y="' + y + '" z="' + z + '"';
	}

	public inline function cross( v1:Point, v2:Point ) {
		this.x = ( v1.y * v2.z - v1.z * v2.y );
		this.y = ( v1.z * v2.x - v1.x * v2.z );
		this.z = ( v1.x * v2.y - v1.y * v2.x );
		return this;
	}

	public inline function dot( v2:Point ) {
		return ( this.x*v2.x + this.y*v2.y + this.z*v2.z );
	}

	public inline function sub( v1:Point, v2:Point ) {
		this.x = ( v1.x - v2.x );  
		this.y = ( v1.y - v2.y );
		this.z = ( v1.z - v2.z );
		return this;
	}


}
